<template>
    <div>
        <el-form ref="form" :model="form" label-width="120px" :rules="rules">
            <el-row>
                <el-col :span="12">
                    <el-form-item label="菜单中文名称" prop="cnName">
                        <el-input v-model="form.cnName"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="菜单路径" prop="path">
                        <el-input v-model="form.path"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="菜单组件" prop="component">
                        <el-input v-model="form.component"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="图标" prop="icon">
                        <el-input v-model="form.icon"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="英文名称" prop="name">
                        <el-input v-model="form.name"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
    </div>
</template>

<script>
    import {insertMenu}from '@/api/menuApi'
    export default {
        data(){
            return {
                form:{
                    cnName:'',
                    path:'',
                    component:'',
                    icon:'',
                    name:''
                },
                rules: {
                    cnName: [
                        { required: true, message: '菜单中文名称不可为空', trigger: 'blur' },
                    ],
                    path: [
                        { required: true, message: '菜单路径不可为空', trigger: 'blur' },
                    ],
                    component: [
                        { required: true, message: '菜单组件不可为空', trigger: 'blur' }
                    ],
                    name: [
                        { required: true, message: '英文名称不可为空', trigger: 'blur' }
                    ],
                }
            }
        },
        methods:{
            //提交
            submit () {
                return new Promise((resolve, reject) => {
                    this.$refs['form'].validate((valid) => {
                        if (valid) {
                            insertMenu(this.form).then(res => {
                                resolve(res.data)
                            }).catch(e => {
                                reject(e)
                                this.$message.error({
                                    message: '抱歉，新增父菜单遇到异常，请重试或联系管理员处理'
                                })
                            })
                        } else {
                            this.$message.error('业务校验未通过')
                            reject(new Error('业务校验失败'))
                        }
                    })
                })
            }
        }
    }
</script>

<style scoped>

</style>
